package com.wisezone.java1902.d13_sort;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.Collection;
import java.util.Collections;
import java.util.Comparator;
import java.util.List;

public class StudentBeanList {
	private String name; // 姓名
	private int age; // 年龄
	private float score; // 成绩

	public StudentBeanList(String name, int age, float score) {
		this.name = name;
		this.age = age;
		this.score = score;
	}

	public String toString() {
		return name + "\t" + age + "\t" + score;
	}

	public static void main(String[] args) {
		StudentBeanList s1 = new StudentBeanList("小明", 23, 89);
		StudentBeanList s2 = new StudentBeanList("小丽", 22, 79);
		StudentBeanList s3 = new StudentBeanList("小强", 23, 99);
		StudentBeanList s4 = new StudentBeanList("小张", 23, 88);
		StudentBeanList s5 = new StudentBeanList("李四", 23, 90);

		// 对以上学生进行排序,按照年龄由低到高排在按成绩由高到低
		List<StudentBeanList> lists = new ArrayList<StudentBeanList>();
		lists.add(s1);
		lists.add(s2);
		lists.add(s3);
		lists.add(s4);
		lists.add(s5);

		for (StudentBeanList student : lists) {
			System.out.println(student);
		}

		// o1和o2，如果函数的返回值为正值，把o1排在o2后面；返回值为负值，
		// 把o1排在o2前面。如果返回值是0，按照容器之前的顺序排列。
		Collections.sort(lists, new Comparator<StudentBeanList>() {
			public int compare(StudentBeanList o1, StudentBeanList o2) {
				float score = o1.score - o2.score;
				if (score > 0) {
					return 1;
				} else if (score < 0) {
					return -1;
				} else {
					return 0;
				}

			};
		}); // 排序

		System.out.println("排序之后");

		for (StudentBeanList student : lists) {
			System.out.println(student);
		}
	}

}
